extends CenterContainer


onready var z_op = $PanelContainer/VBoxContainer/z/OptionButton
onready var collision_op = $PanelContainer/VBoxContainer/collision/OptionButton

onready var dialog_op = $PanelContainer/VBoxContainer/dialog/DialogSelect
onready var res_input = $PanelContainer/VBoxContainer/res/LineEdit
onready var res_frame_input = $PanelContainer/VBoxContainer/resFrame/SpinBox
onready var fps_input = $PanelContainer/VBoxContainer/fps/SpinBox
onready var x_offset_input = $PanelContainer/VBoxContainer/x_offset/SpinBox
onready var y_offset_input = $PanelContainer/VBoxContainer/y_offset/SpinBox
onready var z_index_input = $PanelContainer/VBoxContainer/z_index/SpinBox

var edit_data = null


func _ready():
	dialog_op.add_item("无", -2)

func set_edit_data(data):
	edit_data = data
	
	var map_view = MainTool.getUILayer().get_ui("MapView")
	var position_info = map_view.get_select_position_info()
	var door_list = DataSource.get_data_list(DataSource.map_door)
			
	if edit_data:
		$PanelContainer/VBoxContainer/position/position.text = str("(",data.x, " ", data.y, ")")
		z_op.select(z_op.get_item_index(data["layer"]))
		
		res_input.text = edit_data["res"]
		res_frame_input.value = edit_data["frame"]
		fps_input.value = edit_data["fps"]
		x_offset_input.value = edit_data["xOffset"]
		y_offset_input.value = edit_data["yOffset"]
		z_index_input.value = edit_data["zIndex"]
		
		dialog_op.select_value(edit_data["dialogId"])
		
		collision_op.select(collision_op.get_item_index(edit_data["collision"]))
		
		$PanelContainer/VBoxContainer/modalTitle.text = "编辑装饰"
	else:
		pass
		

func state_update():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	# 更新当前坐标
	var position_info = map_view.get_select_position_info()
	$PanelContainer/VBoxContainer/position/position.text = str("(",position_info.x, " ", position_info.y, ")")
	

func _on_ok_button_up():
	var map_view = MainTool.getUILayer().get_ui("MapView")
	
	var position_info = map_view.get_select_position_info()
	
	
	
	if edit_data:
		read_form(edit_data)
		
		DataSource.update_data(DataSource.map_decoration, edit_data)
		
		MainTool.toast_info("更新装饰成功!")
	else:
		var data = {
			"x": position_info.x,
			"y": position_info.y,
			"mapId": position_info["map_register"]["id"]
		}
		
		read_form(data)
		
		var pre_list = DataSource.get_data_by_list(DataSource.map_decoration, position_info["map_register"]["id"], "mapId")
		for pre in pre_list:
			if pre["x"] == position_info.x and pre["y"] == position_info.y:
				MainTool.toast_info("该位置不可重复添加!")
				return
	
		DataSource.add_data(DataSource.map_decoration, data)
		
		MainTool.toast_info("添加装饰成功!")
	
	map_view.entity_update()
	
	MainTool.getUILayer().pop_ui()


func read_form(dic):
	dic["z"] = 1
	dic["layer"] = z_op.get_selected_id()
	dic["collision"] = collision_op.get_selected_id()
	dic["dialogId"] = dialog_op.get_value()
	dic["res"] = res_input.text
	dic["frame"] = res_frame_input.value
	dic["fps"] = fps_input.value
	dic["xOffset"] = x_offset_input.value
	dic["yOffset"] = y_offset_input.value
	dic["zIndex"] = z_index_input.value
	

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()
